<?php

class CommonAction extends Action
{
	/* (non-PHPdoc)
	 * @see Action::__construct()
	 */
	public function __construct() {
		parent::__construct();
		$this->check();
		$this->assignUser();
	}
	
	public function check()
	{
		if(!isset($_SESSION['user']) && !isset($_SESSION['admin']))
		{
			$this->redirect("Login/loginPage");
			return false;
		}
		return true;
	}
	
	public function checkAdmin() {
		if(!isset($_SESSION['admin']))
		{
			$this->redirect("Login/loginPage");
			return false;
		}
		return true;
	}
	
	public function assignUser()
	{
		$user_dao = new UserModel();
		$user = $user_dao->getById($_SESSION['user']['id']);
		$group_dao = new GroupModel();
		$user['group_ids'] = $group_dao->getGroupIdsByUserId($_SESSION['user']['id']);
		$friend_dao = new FriendModel();
		$friend_ids = $friend_dao->getFriendIdsByUserId($_SESSION['user']['id']);
		$user['friend_ids'] = $friend_ids;
		$this->assign("user", $user);
	}
	
	protected function assignBook($book_id) {
		$book_dao = new BookModel();
		$book = $book_dao->getById($book_id);
		$this->assign("book", $book);
	}
	
}